import { createWebHistory, createRouter } from 'vue-router'
/* Layout */
import Layout from '@/layout'

/**
 * Note: 路由配置项
 *
 * hidden: true                     // 当设置 true 时，路由不在侧边栏显示
 * alwaysShow: true                 // 子路由大于1个时强制显示根路由
 * redirect: noRedirect             // 面包屑不可点击
 * name:'router-name'               // 必须填写，用于<keep-alive>缓存
 * permissions: ['a:a:a']           // 访问权限
 * meta : {
 noCache: true                   // 不被<keep-alive>缓存
 title: 'title'                  // 侧边栏和面包屑显示名称
 icon: 'svg-name'                // 图标路径
 breadcrumb: false               // 不在面包屑显示
 activeMenu: '/system/user'      // 高亮指定侧边栏
 }
 */

// 公共路由（初始化即注册，包含你的所有自定义路由，与原有版本结构一致）
export const constantRoutes = [
  {
    path: '/redirect',
    component: Layout,
    hidden: true,
    children: [
      {
        path: '/redirect/:path(.*)',
        component: () => import('@/views/redirect/index.vue')
      }
    ]
  },
  {
    path: '/login',
    component: () => import('@/views/login'),
    hidden: true
  },
  {
    path: '/register',
    component: () => import('@/views/register'),
    hidden: true
  },
  {
    path: "/:pathMatch(.*)*",
    component: () => import('@/views/error/404'),
    hidden: true
  },
  {
    path: '/401',
    component: () => import('@/views/error/401'),
    hidden: true
  },

  // ---------------------- 你的自定义路由（完全保留原有位置和结构）----------------------
  {
    path: '/arrange-class',
    component: Layout,
    hidden: true,
    children: [{
      path: '',
      name: 'ArrangeClassPage',
      component: () => import('@/views/trainingManagement/programPourses/constructionCurriculum.vue'),
      meta: { title: '开课编排', activeMenu: '/trainingManagement/programPourses/constructionCurriculum' }
    }]
  },
  {
    path: '/trainingManagement-information',
    component: Layout,
    hidden: true,
    children: [{
      path: '',
      name: 'TrainingManagementInformation',
      component: () => import('@/views/trainingManagement/trainingManagement/trainingManagementInformation.vue'),
      meta: { title: '培养方案详情', activeMenu: '/trainingManagement/trainingManagement/trainingManagementInformation' }
    }]
  },
  {
    path: '/designated-program',
    component: Layout,
    hidden: true,
    children: [{
      path: '',
      name: 'DesignatedProgram',
      component: () => import('@/views/trainingManagement/trainingManagement/designatedProgram.vue'),
      meta: { title: '培养方案指定1', activeMenu: '/trainingManagement/trainingManagement/designatedProgram' }
    }]
  },
  {
    path: '/courseAdjustmentRecord',
    component: Layout,
    hidden: true,
    children: [{
      path: '',
      name: 'courseAdjustmentRecord',
      component: () => import('@/views/trainingManagement/MakeUpClass/courseAdjustmentRecord.vue'),
      meta: { title: '选择', activeMenu: '/trainingManagement/MakeUpClass/courseAdjustmentRecord' }
    }]
  },
  {
    path: '/detailedCourseInformation',
    component: Layout,
    hidden: true,
    children: [{
      path: '',
      name: 'detailedCourseInformation',
      component: () => import('@/views/trainingManagement/details/detailedCourseInformation.vue'),
      meta: { title: '课程详情', activeMenu: '/trainingManagement/details/detailedCourseInformation' }
    }]
  },
  {
    path: '/ScoreInput',
    component: Layout,
    hidden: true,
    children: [
      {
        path: '/training/score-input',
        name: 'ScoreInput',
        component: () => import('@/views/trainingManagement/details/scoreInput.vue'),
        meta: {
          title: '成绩录入',
          activeMenu: '/trainingManagement/details/scoreInput'
        }
      }
    ]
  },
  {
    path: '/trainingManagement/studentScore',
    component: Layout,
    hidden: true,
    children: [
      {
        path: '',
        name: 'StudentScoreDisplay',
        component: () => import('@/views/trainingManagement/studentScoreDisplay/index.vue'),
        meta: {
          title: '成绩显示',
          activeMenu: '/trainingManagement/studentScore'
        }
      }
    ]
  },
  {
    path: '/trainingManagement/scheduleDisplay',
    component: Layout,
    hidden: true,
    children: [
      {
        path: '',
        name: 'scheduleDisplay',
        component: () => import('@/views/trainingManagement/scheduleDisplay/index.vue'),
        meta: {
          title: '课程表展示',
          activeMenu: '/trainingManagement/scheduleDisplay'
        }
      }
    ]
  },
  {
    path: '/course/detail/:id?',
    component: Layout,
    hidden: true,
    children: [{
      path: '',
      name: 'CourseDetailWithId', // 修复：与下方路由名称重复问题
      component: () => import('@/views/course/course/detail.vue'),
      meta: { title: '课程详情', activeMenu: '/course/course/detail' }
    }]
  },

  // ---------------------- 原有公共路由（保留）----------------------
  {
    path: '',
    component: Layout,
    redirect: '/index',
    children: [
      {
        path: '/index',
        component: () => import('@/views/index'),
        name: 'Index',
        meta: { title: '首页', icon: 'dashboard', affix: true }
      }
    ]
  },
  {
    path: '/user',
    component: Layout,
    hidden: true,
    redirect: 'noredirect',
    children: [
      {
        path: 'profile',
        component: () => import('@/views/system/user/profile/index'),
        name: 'Profile',
        meta: { title: '个人中心', icon: 'user' }
      }
    ]
  },
  // 新增：原dynamicRoutes中需要在constantRoutes中的/workflow路由（根据你的需求补充）
  {
    path: "/flowMsg/msgTemp",
    component: () => import("@/views/workflow/flowMsg/msgTemplete/index.vue"),
    hidden: false,
  },
  {
    path: "/system",
    name: "system",
    hidden: false,
    component: () => import("@/views/system/user/index.vue"),
  },
];

// 动态路由（仅包含需要权限控制的路由，与原有版本结构一致）
export const dynamicRoutes = [
  {
    path: '/system/user-auth',
    component: Layout,
    hidden: true,
    permissions: ['system:user:edit'],
    children: [
      {
        path: 'role/:userId(\\d+)',
        component: () => import('@/views/system/user/authRole'),
        name: 'AuthRole',
        meta: { title: '分配角色', activeMenu: '/system/user' }
      }
    ]
  },
  {
    path: '/system/role-auth',
    component: Layout,
    hidden: true,
    permissions: ['system:role:edit'],
    children: [
      {
        path: 'user/:roleId(\\d+)',
        component: () => import('@/views/system/role/authUser'),
        name: 'AuthUser',
        meta: { title: '分配用户', activeMenu: '/system/role' }
      }
    ]
  },
  {
    path: '/system/dict-data',
    component: Layout,
    hidden: true,
    permissions: ['system:dict:list'],
    children: [
      {
        path: 'index/:dictId(\\d+)',
        component: () => import('@/views/system/dict/data'),
        name: 'Data',
        meta: { title: '字典数据', activeMenu: '/system/dict' }
      }
    ]
  },
  {
    path: '/monitor/job-log',
    component: Layout,
    hidden: true,
    permissions: ['monitor:job:list'],
    children: [
      {
        path: 'index/:jobId(\\d+)',
        component: () => import('@/views/monitor/job/log'),
        name: 'JobLog',
        meta: { title: '调度日志', activeMenu: '/monitor/job' }
      }
    ]
  },
  {
    path: '/tool/gen-edit',
    component: Layout,
    hidden: true,
    permissions: ['tool:gen:edit'],
    children: [
      {
        path: 'index/:tableId(\\d+)',
        component: () => import('@/views/tool/gen/editTable'),
        name: 'GenEdit',
        meta: { title: '修改生成配置', activeMenu: '/tool/gen' }
      }
    ]
  },
  // 课程详情（无参数）路由（保留在dynamicRoutes中，与原有版本一致）
  {
    path: '/course/detail',
    component: Layout,
    hidden: true,
    children: [{
      path: '',
      name: 'CourseDetail', // 与带参数路由名称区分，避免重复
      component: () => import('@/views/trainingManagement/details/detailedCourseInformation.vue'),
      meta: {
        title: '课程详情',
        activeMenu: '/trainingManagement/details'
      }
    }]
  },
  // ---------------------- 工作流相关动态路由（从你的需求补充，确保权限控制）----------------------
  {
    path: "/startFlow/index",
    component: Layout,
    hidden: true,
    permissions: ["workflow"],
    children: [
      {
        path: "/startFlow/index",
        component: () => import("@/views/workflow/startFlow/index"),
        name: "startFlow",
        meta: {
          title: "发起流程申请",
          activeMenu: "/taskCenter",
        },
      },
    ],
  },
  {
    path: "/startOutside/index",
    component: Layout,
    hidden: true,
    permissions: ["workflow"],
    children: [
      {
        path: "/startOutside/index",
        component: () => import("@/views/workflow/startOutside/index"),
        name: "startOutside",
        meta: {
          title: "Saas流程申请",
          activeMenu: "/taskCenter",
        },
      },
    ],
  },
  {
    path: "/workflow/diy-design",
    component: Layout,
    hidden: true,
    permissions: ["workflow"],
    children: [
      {
        path: "/workflow/diy-design",
        component: () => import("@/views/workflow/flowDesign/diy"),
        name: "diy-design",
        meta: {
          title: "DIY流程设计",
          activeMenu: "/workflow/flowlist",
        },
      },
    ],
  },
  {
    path: "/workflow/lf-design",
    component: Layout,
    hidden: true,
    permissions: ["workflow"],
    children: [
      {
        path: "/workflow/lf-design",
        component: () => import("@/views/workflow/flowDesign/lf"),
        name: "lf-design",
        meta: {
          title: "LF流程设计",
          activeMenu: "/workflow/flowlist",
        },
      },
    ],
  },
  {
    path: "/workflow/flow-version",
    component: Layout,
    hidden: true,
    permissions: ["workflow"],
    children: [
      {
        path: "/workflow/flow-version",
        component: () => import("@/views/workflow/flowList/version"),
        name: "version",
        meta: {
          title: "版本管理",
          activeMenu: "/workflow/flowlist",
        },
      },
    ],
  },
  {
    path: "/workflow/flowPreview",
    component: Layout,
    hidden: true,
    permissions: ["workflow"],
    children: [
      {
        path: "/workflow/flowPreview",
        component: () => import("@/views/workflow/flowPreview/index"),
        name: "flowPreview",
        meta: {
          title: "流程预览",
          activeMenu: "/workflow/flowlist",
        },
      },
    ],
  },
  {
    path: "/outsideMgt/preview",
    component: Layout,
    hidden: true,
    permissions: ["workflow"],
    children: [
      {
        path: "/outsideMgt/preview",
        component: () => import("@/views/workflow/outsideMgt/preview/index"),
        name: "outsideFlowPreview",
        meta: {
          title: "Saas流程预览",
          activeMenu: "/outsideMgt/outsideTemp",
        },
      },
    ],
  },
  {
    path: "/outsideMgt/outsideDesign",
    component: Layout,
    hidden: true,
    permissions: ["workflow"],
    children: [
      {
        path: "/outsideMgt/outsideDesign",
        component: () => import("@/views/workflow/outsideMgt/outsideDesign/index"),
        name: "outsideFlowDesign",
        meta: {
          title: "Saas流程复制",
          activeMenu: "/outsideMgt/outsideTemp",
        },
      },
    ],
  },
  {
    path: "/outsideMgt/app-setting",
    component: Layout,
    hidden: true,
    permissions: ["workflow"],
    children: [
      {
        path: "/outsideMgt/app-setting",
        component: () => import("@/views/workflow/outsideMgt/outsideApp/Setting/index"),
        name: "app-setting",
        meta: {
          title: "应用配置",
          activeMenu: "/outsideMgt/outsideApp",
        },
      },
    ],
  },
  {
    path: "/workflow/instance/removeSign",
    component: Layout,
    hidden: true,
    permissions: ["workflow"],
    children: [
      {
        path: "processNumber/:processNumber",
        component: () => import("@/views/workflow/flowTask/instance/flowRemoveSign"),
        name: "removeSign",
        meta: { title: "流程减签", activeMenu: "/workflow/instance" },
      },
    ],
  },
  {
    path: "/workflow/instance/addSign",
    component: Layout,
    hidden: true,
    permissions: ["workflow"],
    children: [
      {
        path: "processNumber/:processNumber",
        component: () => import("@/views/workflow/flowTask/instance/flowAddSign"),
        name: "addSign",
        meta: { title: "流程加签", activeMenu: "/workflow/instance" },
      },
    ],
  },
  {
    path: "/workflow/instance/changeSign",
    component: Layout,
    hidden: true,
    permissions: ["workflow"],
    children: [
      {
        path: "processNumber/:processNumber",
        component: () => import("@/views/workflow/flowTask/instance/flowChangeSign"),
        name: "changeSign",
        meta: { title: "变更审批人", activeMenu: "/workflow/instance" },
      },
    ],
  },
];

// 路由实例（使用createWebHistory，与原有版本一致）
const router = createRouter({
  history: createWebHistory(), // 保持与原有版本相同的history模式
  routes: constantRoutes,
  scrollBehavior(to, from, savedPosition) {
    if (savedPosition) {
      return savedPosition
    }
    return { top: 0 }
  },
});

export default router;
